// pages/recording/recording.js
const { getMonthRecord, getRecordInformation } = require('../../api/record')
const { getDateObj } = require("../../utils/date")
let app =  getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 标签栏
    isMonthRecord:true,
    isNothing:false,
    // 是否痊愈0=是1=否
    status:0,
    date: '',
    show: true,
    goods:false,
    // 表情
    moodPicList:{
      smile:{ active:'../../static/iconfont/recoding/emoji/smaile_2.svg',
              normal:'../../static/iconfont/recoding/emoji/smaile.svg'
              },
      cry:{active:'../../static/iconfont/recoding/emoji/cry.svg',
            normal:'../../static/iconfont/recoding/emoji/cry_2.svg'
          },
      angry:{active:'../../static/iconfont/recoding/emoji/angry.svg',
            normal:'../../static/iconfont/recoding/emoji/angry_2.svg'
          },
      laugh:{active:'../../static/iconfont/recoding/emoji/laugh.svg',
            normal:'../../static/iconfont/recoding/emoji/laugh_2.svg'
          },
      sad:{active:'../../static/iconfont/recoding/emoji/sad.svg',
            normal:'../../static/iconfont/recoding/emoji/sad_2.svg'
          },
    },
    // 心情
    mood:'',
    active:0,
    tag_active:10,
    showTemplate:false,
    // 体温
    template:'记录体温',
    temperature:['37','37.5','37.6','37.7','37.8','37.9','38','38以上'],
    // 关于顶部导航栏
    nvabarData: {
      showCapsule: 0, //是否显示左上角图标   1表示显示    0表示不显示
      title: '月记录', //导航栏 中间的标题
      white: false, // 是就显示白的，不是就显示黑的。
      bgColor:'#B7EADE' // 加个背景 不加就是没有
    },
    // 导航头的高度
    height: app.globalData.height * 2
  },
/**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  onShow: function () {
    /**
     * 处理获取所有提醒
     */
    // 处理今日日期，把得出的2021/x/x处理成2021-x-x
    let isToday =getDateObj(new Date()).dataStr.replace(/\//g,'-')
    let Today = isToday.substring(0,isToday.length-3)
    let record_list = [];

    getMonthRecord({date:Today})
      .then(res=>{
        let { info } = res;
        let { thisMonth } = info;
        let { diaryList } = info;
        let { List } = diaryList;
        let taskList = thisMonth.List;
        // 加载的时候显示今日的提醒
        taskList.forEach(item=>{
          if(isToday == item.remind_time_day){
            record_list.push(item)
          }
        })
        // 设置所有提醒卡片
        this.setData({record_tag_list:List})
      })
      .then(()=>{
        if(record_list.length == 0){
          // 没有提醒则显示缺省图
          this.setData({isNothing:true})
        }else{
          this.setData({record_list,isNothing:false})
        }
      })

    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        // 当前页面的 tabBar 索引
        active: 1
      })
    };
  },


  // 点击月记录
  selectMonthRecord(){
    this.setData({isMonthRecord:true,selected:null})
  },
  // 显示温度
  showTemplatePick(){
    this.setData({showTemplate:true})
  },

  templatePickConfirm(event){
    const { value, index } = event.detail
    this.setData({
      template:value,
      showTemplate:false
    })
  },

  templatePickCancel(){
    this.setData({showTemplate:false})
  },

  // tab栏切换
  onChange(e){
    let isMonthRecord = e.detail.active == 0
    this.setData({isMonthRecord})
  },

  change(e){
    let record_list = e.detail.taskList;
    this.setData({selectedDate:e.detail.dataStr})
    if(!record_list){
      this.setData({isNothing:true})
    }else{
      this.setData({record_list,isNothing:false})
    }
  },

  handelY(){
    this.setData({
      left: 0,
      status:0
    })
  },

  changeMood(e){
    this.setData({
      mood: e.detail,
    });
  },

  handelN(){
    this.setData({
      left: 100,
      status:1
    })
  },

  toDaylily(){
    wx.navigateTo({
      url:'daylily'
    })
  },

  toInformation(){
    wx.navigateTo({
      url:'infomation/index?id='+this.data.activeRecordId
    })
  },

  // 点击选择提醒
  handelSelect(e){
    this.setData({
      activeRecordId:e.currentTarget.dataset.info.remind_ID,
      selected:e.target.dataset.index,
      isMonthRecord:false,
      active:false
    })
  }
})